﻿// 2. Write a program that reads a rectangular matrix of size N x M and finds in it the square 3 x 3 that has maximal sum 
// of its elements.

using System;

class MaxSumInRecatngularMatrix
{
    static void Main()
    {
        // get the row number of the matrix
        Console.Write("Enter the number of rows of the matrix : ");
        string rowsStr = Console.ReadLine();
        int rows;
        while (!int.TryParse(rowsStr, out rows))
        {
            Console.Write("Invalid input. Enter again the number of rows of the matrix : ");
            rowsStr = Console.ReadLine();
        }

        // get the column number of the matrix
        Console.Write("Enter the number of columns of the matrix : ");
        string columnsStr = Console.ReadLine();
        int columns;
        while (!int.TryParse(columnsStr, out columns))
        {
            Console.Write("Invalid input. Enter again the number of columns of the matrix : ");
            columnsStr = Console.ReadLine();
        }

        // define the matrix
        int[,] matrix = new int[rows, columns];
        // enter values for the cells
        Console.WriteLine("Enter the cells of the matrix:");
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < columns; col++)
            {
                Console.Write("matrix[{0},{1}] = ", row, col);
                matrix[row, col] = int.Parse(Console.ReadLine());
            }
        }

        // the variable that will keep the best sum
        int maxSum = int.MinValue;
        int bestRow = 0;
        int bestCol = 0;
        
        if (rows < 3 || columns < 3)
        {
            Console.WriteLine("Invalid input. The dimensions should be greater than three. ");
        }
        else
        {
            for (int row = 0; row < matrix.GetLength(0) - 2; row++)
            {
                for (int col = 0; col < matrix.GetLength(1) - 2; col++)
                {
                    int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
                              matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
                              matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
                    if (sum > maxSum)
                    {
                        maxSum = sum;
                        bestRow = row;
                        bestCol = col;
                    }
                }
            }

            // print the result
            Console.WriteLine("The maximum sum is {0}", maxSum);
            Console.WriteLine("The square 3x3 with maximum sum is :");
            for (int row = bestRow; row < bestRow + 3; row++)
            {
                for (int col = bestCol; col < bestCol + 3; col++)
                {
                    Console.Write("{0,5}", matrix[row, col]);
                }
                Console.WriteLine();
            }
        }
        
    }
}
